    <?php

class Station extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Mstation');
        $this->_data['controller'] = 'station';
    }

    public function index() {

        //Pre-load View
        $this->_data['load_page'] = 'station/index';
        $this->_data['action']  = 'index';

        //Process
        if ($this->input->post('yt0')) {
            //Form Validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('Station[name]', 'Station name', 'required');
            if ($this->form_validation->run()) {
                $station = $this->input->post('Station');
                $this->Mstation->insertStation($station);
                $this->_data['data']['message'] = 'Save station successful';
            } else {
                $this->_data['data']['error'] = validation_errors('<li class="errorMessage">','</li>');
            }
        }
        //Load View
        $this->load->view($this->_data['template'], $this->_data);
    }

    public function listStation($start) {
        //Pre-load View
        $this->_data['load_page'] = 'station/listStation';
        $this->_data['action']  = 'listStation';
        //GET
        $start = isset($start)?$start:0;
        $total_pages = count($this->Mstation->getStations());
        $url = base_url().'/station/listStation';
        $params = array(
            'start' =>  $start
        );
        $stations = $this->Mstation->getStations($params);
        
        //Process
        
        $this->_data['load_page'] = 'station/listStation';
        $this->_data['data']['stations'] = $stations;
        $this->_data['data']['pagination']= $this->getPagination($total_pages, $url);  
        $this->load->view($this->_data['template'], $this->_data);
    }

    public function manage() {
        //Pre-load View
        $this->_data['load_page'] = 'station/manage';
        $this->_data['action']  = 'manage';

        //Process
        $this->load->model('Mline');
        $lines = $this->Mline->getLines();
        $stations = $this->Mstation->getStations();
        $this->_data['data']['lines'] = $lines;
        $this->_data['data']['stations'] = $stations;
        $this->_data['load_page'] = 'station/manage';
        $this->_data['data']['html_select_box_lines'] = $this->getLinesInSelectBoxHTML($lines);
        $this->load->view($this->_data['template'], $this->_data);
    }
    private function getLinesInSelectBoxHTML($lines) {
        $html = '<select name="id_line"><option value="0">None</option>';

        $html .= '</select>';
        return $html;
    }

    public function edit() {
        $this->_data['action'] = 'edit';
        //Process
        if ($this->input->post('yt0')) {
            //Form Validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('Station[name]', 'Station name', 'required');
            if ($this->form_validation->run()) {
                $station =  $this->input->post('Station');
                $station['id'] = $_GET['id'];
                $this->Mstation->update($station);

                $this->_data['data']['station'] = $station;
                $this->_data['load_page'] = 'station/index';
                $this->_data['message'] = 'Save station successful';
                $this->load->view($this->_data['template'], $this->_data);
            } else {
                $station =  $this->Mstation->getStation($_GET['id']);
                $this->_data['data']['station'] = $station;
                $this->_data['load_page'] = 'station/index';
                $this->_data['data']['error'] = validation_errors('<li class="errorMessage">','</li>');
                $this->load->view($this->_data['template'], $this->_data);
            }
        } else {
            $id = isset($_GET['id']) ? (int) $_GET['id'] : NULL;
            if (is_null($id)) {
                $this->listStation();
            } else {
                $station = $this->Mstation->getStation($id);
                $this->_data['data']['station'] = $station;
                $this->_data['load_page'] = 'station/index';
                $this->load->view($this->_data['template'], $this->_data);
            }
        }
    }
    public function delete() {
        $id = isset($_GET['id'])?$_GET['id']:NULL;        
        if (!is_null($id)) {
            $station =  $this->Mstation->getStation($id);
            $this->_data['data']['station'] = $station;
            $this->_data['load_page'] = 'station/delete';
            $this->load->view($this->_data['template'], $this->_data);
            //POST
            if ($this->input->post('delete_ok')) {
                $this->Mstation->delete($id);
                redirect(base_url().'/station/manage');
            } elseif ($this->input->post('delete_no')) {
                redirect(base_url().'/station/manage');
            }
        } else {
            $this->manage();
        }
    }
}

?>